<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Maddress extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
	
    public function getListAddress($number = 0,$offset = 0){
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT a.id, a.name, a.address, a.lat, a.long, d.name AS district, p.name AS province FROM tbl_address AS a LEFT JOIN district AS d ON d.districtid = a.id_district LEFT JOIN province AS p ON p.provinceid = a.id_province";
        
        if($this->session->userdata('level') == 2){
            $sql .= " LEFT JOIN manage_group_key AS gk ON gk.group_id = a.group_id WHERE gk.user_id = ".$this->session->userdata('id'); 
        }
        
        $sql .= " ORDER BY a.created DESC";
        $count = $this->db->query($sql)->num_rows();
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        return array('data' => $xs, 'count' => $count);
    }
    
    public function getAddressDetail($id){
        $sql = "SELECT * FROM tbl_address WHERE id = ".$id;
        $data = $this->db->query($sql)->row_object();
        
        return $data;
    }
    
    public function addAddress($data){
        $data['created'] = time();
        $this->db->insert('tbl_address', $data);
        
        $this->syncInsertAddress($data,$this->db->insert_id());
        return 200;
    }
    
    public function editAddress($id,$data){
        $this->db->update('tbl_address', $data, "id = ".$id);
        $this->syncUpdateAddress($data,$id);
        return 200;
    } 
    
    public function deleteAddress($id){
        $this->db->delete('tbl_address', "id = ".$id);
        return 200;
    }
    
    public function getGroup(){
	    if($this->session->userdata('level') == 2){
           $group =  $this->db->query('SELECT tg.id, tg.name FROM tbl_group AS tg LEFT JOIN manage_group_key AS gk ON gk.group_id = tg.id WHERE gk.user_id = '.$this->session->userdata('id'))->result_object();
	    }else{
	       $group = $this->db->query('SELECT id, name FROM tbl_group WHERE 1=1')->result_object();
	    }
        
        $arr = array('0' => 'Chọn một nhóm mạng');
        foreach($group AS $key => $value){
            $arr[$value->id] = $value->name;
        }
        return $arr;
	}
    
    /**
     * @void Sync address to Wicity
     * ---------------------------------------------
     */ 
     
     private function syncInsertAddress($data,$idplace){
        $this->db_wicity = $this->load->database('wicitydb', TRUE);
        
        $place_wicity = $this->db_wicity->query('SELECT id FROM place WHERE id_address_sync = ' . $idplace);
        if ($place_wicity->num_rows() == 0) {
            $dr = array(
                'id_address_sync' => $idplace,
                'name' => $data['name'],
                'address' => $data['address'],
                'lat' => $data['lat'],
                'long' => $data['long'],
                'province_id' => $data['id_province'],
                'district_id' => $data['id_district']
            );

            $this->db_wicity->insert('place', $dr);
            $id_p = $this->db_wicity->insert_id();
            
            $ads_type = $this->db_wicity->query("SELECT id FROM ads_type")->result_object();
            foreach($ads_type AS $key => $value){
                $this->db_wicity->insert('place_ads_price', array('place_id' => $id_p, 'ads_type_id' => $value->id, 'price_per_view' => 0, 'active' => 1));
            }
            
        } else {
            $dr = array(
                'name' => $data['name'],
                'address' => $data['address'],
                'lat' => $data['lat'],
                'long' => $data['long'],
                'province_id' => $data['id_province'],
                'district_id' => $data['id_district']
            );
            $id_p = $place_wicity->row_object()->id;
            $this->db_wicity->update('place', $dr, "id = " . $id_p);
        }
        
        return 200;
     }
     
     private function syncUpdateAddress($data,$idplace){
        $this->db_wicity = $this->load->database('wicitydb', TRUE);
        
        $dr = array(
            'name' => $data['name'],
            'address' => $data['address'],
            'lat' => $data['lat'],
            'long' => $data['long'],
            'province_id' => $data['id_province'],
            'district_id' => $data['id_district']
        );
        
        $this->db_wicity->update('place', $dr, "id_address_sync = ".$idplace);
        
        return 200;
     }
}
